package com.hnevc.zz.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.hnevc.zz.R;
import com.hnevc.zz.database.RechargeDatabase;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RechargeRecordActivity extends AppCompatActivity {
    private Spinner mSpnBill;
    private Button mBtnBillQuery,back;
    private ListView mListviewBill;
    MyAdapter adapter;
    List<Map> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_record);
        initView();
        initData();
        getData();
        Collections.reverse(list);
        adapter.notifyDataSetInvalidated();
    }
    private void initData() {
        list = new ArrayList<>();
        adapter = new MyAdapter(list);
        mListviewBill.setAdapter(adapter);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RechargeRecordActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        mBtnBillQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSpnBill.getSelectedItem().toString().equals("时间升序")){
                    getData();
                    adapter.notifyDataSetChanged();
                }else {
                    getData();
                    Collections.reverse(list);
                    adapter.notifyDataSetInvalidated();
                }
            }
        });
    }
    void getData(){
        list.clear();
        RechargeDatabase database =RechargeDatabase.getInstance(getApplicationContext());
        SQLiteDatabase db = database.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from record order by date", null);
        if(cursor.getCount() == 0){
            Toast.makeText(this, "暂无数据", Toast.LENGTH_SHORT).show();
        }
        while (cursor.moveToNext()){
            Map<String,String> map = new HashMap<>();
            map.put("_id",cursor.getString(cursor.getColumnIndex("_id")));
            map.put("phone",cursor.getString(cursor.getColumnIndex("phone")));
            map.put("money",cursor.getString(cursor.getColumnIndex("money")));
            map.put("date",cursor.getString(cursor.getColumnIndex("date")));
            list.add(map);
        }
        db.close();
        database.close();
        cursor.close();
    }
    private void initView() {
        mSpnBill = (Spinner) findViewById(R.id.sp_rerecord);
        mBtnBillQuery = (Button) findViewById(R.id.query_rerecord);
        mListviewBill = (ListView) findViewById(R.id.listview_rerecord);
        back=findViewById(R.id.btn_back_rere);
    }
    class MyAdapter extends BaseAdapter {
        List<Map>list;

        public MyAdapter(List<Map> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            LayoutInflater inflater=LayoutInflater.from(getApplicationContext());
            if (viewHolder==null){
                convertView=inflater.inflate(R.layout.item_listview_rerecord,parent,false);
                viewHolder=new ViewHolder(convertView);
                convertView.setTag(viewHolder);

            }else {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            Map<String,String> map=list.get(position);
            viewHolder.textView1.setText(map.get("_id"));
            viewHolder.textView2.setText(map.get("phone"));
            viewHolder.textView3.setText(map.get("money"));
            viewHolder.textView4.setText(map.get("date"));

            return convertView;
        }
        class ViewHolder{
            View view;
            TextView textView1,textView2,textView3,textView4;
            ViewHolder(View view){
                this.view=view;
                this.textView1 = (TextView) view.findViewById(R.id.id_rerecord);
                this.textView2 = (TextView) view.findViewById(R.id.phone_rerecord);
                this.textView3 = (TextView) view.findViewById(R.id.money_rerecord);
                this.textView4 = (TextView) view.findViewById(R.id.retime_rerecord);

            }
        }
    }
}
